﻿namespace DotNetUtility.ExtensionMethods
{
    using System;

    public static class IntExtensions
    {
        /// <summary>
        /// To increment an int value.
        /// </summary>
        /// <param name="item">The item to increment.</param>
        /// <returns>The incremented value.</returns>
        /// <example>
        ///     <code>
        ///     int itemToIncrement =1;
        ///     var result = itemToIncrement.IncrementByOne();
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static int IncrementByOne(this int item)
        {
            return IncrementBy(item, 1);
        }

        /// <summary>
        /// To increment an int value.
        /// </summary>
        /// <param name="item">The item to increment.</param>
        /// <param name="by">Increment by</param>
        /// <returns>The incremented value.</returns>
        /// <example>
        ///     <code>
        ///     int itemToIncrement =1;
        ///     var result = itemToIncrement.IncrementBy(1);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static int IncrementBy(this int item, int by)
        {
            return item + by;
        }

        /// <summary>
        /// To convert the bool value as 1 or 0.
        /// </summary>
        /// <param name="boolean">The bool value.</param>
        /// <returns>either 1 or 0</returns>
        /// <example>
        ///     <code>
        ///     bool itemToConvert =true;
        ///     var result = itemToConvert.ToBinaryTypeNumber();
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static int ToBinaryTypeNumber(this Boolean boolean)
        {
            return boolean ? 1 : default(int);
        }

        /// <summary>
        /// To find whether an item is in range. The range is defined by start position and endposition.
        /// </summary>
        /// <param name="itemToCheck"></param>
        /// <param name="startPosition"></param>
        /// <param name="endPosition"></param>
        /// <returns>The result whether the item is in the range or not.</returns>
        /// <example>
        ///     <code>
        ///     int itemToCheck =1;
        ///     var result = itemToCheck.InBetween(1,7);
        ///     </code>
        /// </example> 
        /// Contributed by Mohammad Rahman
        public static bool InBetween(this int itemToCheck, int startPosition, int endPosition)
        {
            return itemToCheck >= startPosition && itemToCheck <= endPosition;
        }

        /// <summary>
        /// To check whether a given item is valid index of an array.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="arrayToCheck"></param>
        /// <returns>The result whether the item is valid array index.</returns>
        /// <example>
        ///     <code>
        ///         int itemToCheck = 2;
        ///         int[] items = { 1, 2, 3, 4, 5, 6, 7 };
        ///         var result = itemToCheck.IsIndexInArray(items);       
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool IsIndexInArray(this int index, Array arrayToCheck)
        {
            return index.ToArrayInedx().InBetween(arrayToCheck.GetLowerBound(0), arrayToCheck.GetUpperBound(0));
        }

        /// <summary>
        /// To convert the given position into an array index.
        /// </summary>
        /// <param name="at"></param>
        /// <returns>The index of the array.</returns>
        /// <example>
        ///     <code>
        ///     int itemToCheck =1;
        ///     var result = itemToCheck.ToArrayInedx();
        ///     </code>
        /// </example> 
        /// Contributed by Mohammad Rahman
        public static int ToArrayInedx(this int at)
        {
            return at == default(int) ? default(int) : at - 1;
        }
    }
}
